package com.bw.ivideoproject.video

import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.viewModelScope
import com.bw.ivideoproject.entity.TypeEntity
import com.bw.ivideoproject.entity.VideoEntity
import com.bw.lib_mvvm.BaseViewModel
import kotlinx.coroutines.launch

class VideoViewModel : BaseViewModel() {

    private val model: VideoModel by lazy {
        getModel(VideoModel::class.java)
    }

    val typeLiveData = MediatorLiveData<List<TypeEntity>>()
    val typeByIdLiveData = MediatorLiveData<List<TypeEntity>>()
    val videoLiveData = MediatorLiveData<List<VideoEntity>>()
    val filedLiveData = MediatorLiveData<String>()

    fun getType() {
        viewModelScope.launch {
            val (code, data, msg) = model.getType()
            if (code == 0) {
                typeLiveData.value = data
            } else {
                filedLiveData.value = msg
            }
        }
    }

    fun getTypeById(pid:Int){
        viewModelScope.launch {
            val (code, data, msg) = model.getTypeById(pid)
            if (code == 0) {
                typeByIdLiveData.value = data
            } else {
                filedLiveData.value = msg
            }
        }
    }

    fun getVideoByType(typeId:Int,page:Int,pageSize:Int){
        viewModelScope.launch {
            val (code, data, msg) = model.getVideoByType(typeId, page, pageSize)
            if (code == 0) {
                videoLiveData.value = data
            } else {
                filedLiveData.value = msg
            }
        }
    }


}